package com.wcs.exam.common.util;


import com.wcs.exam.common.constant.BaseConstant;

/**
 * 支付工具类
 *
 * @author wcs
 */
public final class PayUtil {

    public static final String SLASH = "/";

    /**
     * 店铺交易支付回调通知地址
     * /user/callback/order/info/pay/notify/{payChannelCode}
     */
    private static final String PAY_SHOP_NOTIFY_URL = "/v1/callback/order/info/pay/notify/%s";

    /**
     * 获取店铺交易通知URL
     *
     * @param websiteDomain  网站域名
     * @param payChannelCode 支付通道编码
     * @return 店铺交易通知地址
     */
    public static String getShopNotifyUrl(String websiteDomain, String payChannelCode) {
        return getUrl(websiteDomain, BaseConstant.GATEWAY) + String.format(PAY_SHOP_NOTIFY_URL, payChannelCode);
    }


    private static String getUrl(String domain, String url) {
        if (domain.endsWith(SLASH)) {
            if (url.startsWith(SLASH)) {
                return domain.substring(0, domain.length() - 1) + url;
            } else {
                return domain + url;
            }
        } else {
            if (url.startsWith(SLASH)) {
                return domain + url;
            } else {
                return domain + SLASH + url;
            }
        }
    }
}
